﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Text;
using App.Core.Mail;


public partial class Pages_Public_ForgotPassword : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            App.Core.Mail.MailManager.SmtpHost = ConfigReader.SmtpHost;
            App.Core.Mail.MailManager.SmtpPort = ConfigReader.SmtpPort;
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void SendPassword()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var user = context.Users.FirstOrDefault(P => String.Compare(P.LoginName, txtUserName.Text, false) == 0);
        if (user == null)
            WebUtil.ShowMessageBox(divMessage, "Sorry! User Name does not exist.", true);
        else
        {
            if (user.Contact == null) //Individual
                SendForgotPasswordEmail(user.LoginName, user.LoginName, user.Password, user.Customer.Name);
            else            
                SendForgotPasswordEmail(user.Contact.Email, user.LoginName, user.Password, user.Contact.Name);             
        }
    }

    protected void SendForgotPasswordEmail(String toEmail, String userName, String password, String contactName)
    {        
        StringBuilder template = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
        if (template.Length > 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Dear {0},<br/>", contactName);
            sb.AppendFormat("Following is your login information of {0}. Please Keep this email for your future reference.<br /><br />", ConfigReader.ProviderCompany);
            sb.AppendFormat("<b>User Name:</b> {0}<br />", userName);
            sb.AppendFormat("<b>Password:</b> {0}<br /><br />", password);
            sb.AppendFormat("Please click on the link below to login:<br /><a href='{0}{1}'>{0}{1}</a><br /><br />"
                    , WebUtil.GetDomainAddress(), AppConstants.Pages.LOG_IN);
            sb.AppendFormat("<b>Note:</b> If you cannot click on the link then Copy it and Paste to your browser's address bar to login.");

            template.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());
            template.Replace(AppConstants.ETConstants.MESSAGE, sb.ToString());
            String fromEmail = ConfigReader.SupportEmail;
            String subject = String.Format("Your Password of {0}", ConfigReader.ProviderCompany);
            try
            {
                bool isSent = MailManager.SendMail(toEmail, String.Empty, String.Empty, fromEmail, subject, template.ToString());
                //MailManager.SendEmailThroughGmail(fromEmail, toEmail, subject, template.ToString());
                WebUtil.ShowMessageBox(divMessage, String.Format("Your Password has been sent to {0}", toEmail), false);
            }
            catch(Exception ex)
            {
                BtcHousingEntities context = new BtcHousingEntities();
                EmailMessage message = new EmailMessage();
                message.Subject = subject;
                message.FromEmail = fromEmail;
                message.ToEmail = toEmail;
                message.Message = template.ToString();
                message.IsSent = false;
                message.Created = DateTime.Now;
                message.AttemptedOn = DateTime.Now;
                context.AddToEmailMessages(message);
                context.SaveChanges();
                WebUtil.ShowMessageBox(divMessage, String.Format("Sorry! Your Password cannot be sent to {0}. An Internal Error occured and our technical team has been notified about this error. {1}", toEmail, ex.Message), true);
            }
        }
    }
    protected void btnSendPassword_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SendPassword();
        }
    }
}